package com.example.smartcity_40.house;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartcity_40.R;
import com.example.smartcity_40.house.bean.HousedetailsBean;
import com.example.smartcity_40.house.bean.HoustlistBean;
import com.example.smartcity_40.tool.Tool;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class HousedetailsActivity extends AppCompatActivity {

    private HousedetailsActivity context;
    private String houseid;
    private Tool tool;
    private ImageView pic;
    private TextView areaSize;
    private TextView price;
    private TextView sourceName;
    private TextView houseType;
    private TextView description;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_housedetails);
        context = this;
        initview();
        housedetailsmethod();
    }

    private void housedetailsmethod() {
        if (TextUtils.isEmpty(houseid)) {
            return;
        }
        tool.send("/prod-api/api/house/housing/" + houseid,
                Tool.GET,
                null,
                true,
                HousedetailsBean.class,
                new Function1<HousedetailsBean, Unit>() {
                    @Override
                    public Unit invoke(HousedetailsBean housedetailsBean) {
                        if (housedetailsBean.getCode() == 200) {
                            inithouse(housedetailsBean.getData());
                        }
                        return null;
                    }
                });
    }

    private void inithouse(HousedetailsBean.DataBean data) {
        try {
            if(data==null){
                return;
            }

            String dataPic = data.getPic();
            if(!TextUtils.isEmpty(dataPic)){
                Glide.with(context).load(Tool.url+dataPic).into(pic);
            }else {
                Glide.with(context).load(Tool.url+dataPic)
                        .error(R.drawable.fu_img4)
                        .into(pic);
            }

            Integer dataAreaSize = data.getAreaSize();
            if(dataAreaSize !=null){
                areaSize.setText(String.valueOf(dataAreaSize));
            }

            String dataPrice = data.getPrice();
            if(!TextUtils.isEmpty(dataPrice)){
                price.setText(dataPrice);
            }

            String dataSourceName = data.getSourceName();
            if(!TextUtils.isEmpty(dataSourceName)){
                sourceName.setText(dataSourceName);
            }

            String dataHouseType = data.getHouseType();
            if(!TextUtils.isEmpty(dataHouseType)){
                houseType.setText(dataHouseType);
            }

            String dataDescription = data.getDescription();
            if(!TextUtils.isEmpty(dataDescription)){
                description.setText(dataDescription);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void initview() {
        ImageView back = findViewById(R.id.back);
        pic = findViewById(R.id.pic);
        areaSize = findViewById(R.id.areaSize);
        price = findViewById(R.id.price);
        sourceName = findViewById(R.id.sourceName);
        houseType = findViewById(R.id.houseType);
        description = findViewById(R.id.description);
        Intent intent = getIntent();
        houseid = intent.getStringExtra("houseid");
        tool = new Tool(context);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });
    }
}